#include <iostream>
#include "Thread.hpp"
#include "BlockQueue.hpp"
#include <unistd.h>
#include <vector>

using namespace ThreadMoudle;

const int N = 20;

int ticketnum = 10;

int gap = 20;

ThreadMoudle::Mutex gmtx;


void Consumer(BlockQueueMoudle::Blockqueue* bq)
{
    while(true)
    {
        int data;
        // 1. 从bq拿到数据
        bq->Pop(&data);
        
        // 2.做处理
        printf("Consumer, 消费了一个数据: %d\n", data);

    }
}

void Producer(BlockQueueMoudle::Blockqueue* bq)
{
    int data = 10;
    while(true)
    {
        // 1. 从外部获取数据
        sleep(2);

        // 2. 生产到bq中
        bq->Equeue(data);

        printf("producter 生产了一个数据: %d\n", data);
        data++;


    }
}

int main()
{
    std::vector<Thread<BlockQueueMoudle::Blockqueue*>> threads;
    BlockQueueMoudle::Blockqueue* bq = new BlockQueueMoudle::Blockqueue;

    for(int i= 1; i <= 3 ; ++i)
    threads.emplace_back(Consumer,bq);

    for(int i = 1 ; i <= 4 ; ++i)
    threads.emplace_back(Producer,bq);


    for(auto& t : threads)
    t.Start();
    for(auto& t : threads)
    {
        int n =t.Join();
        std::cout<<"join "<<t.Name()<<" = "<<n<<std::endl;
    }

    return 0;
}


